package shopping;

import java.util.Scanner;

/**
 * 购物系统
 * @author 17399
 * @since 2024/11/30 20:44
 */
public class ShoppingCartSystem {
    public static void main(String[] args) {
        goods stock []= new goods[5];
        shoppingCart[] shop = new shoppingCart[10];
        stock[0] = new goods("苹果", "水果", 5.0, 10);
        stock[1] = new goods("鸡尾酒", "饮品", 7.0, 8);
        stock[2] = new goods("蛋糕", "糕点", 15.0, 5);
        stock[3] = new goods("薯片", "零食", 6.0, 20);
        stock[4] = new goods("纸巾", "生活用品", 3.0, 15);
        System.out.println("这里是您的购物车");
        while (true) {
            System.out.println("请选择操作：1.添加商品 2.查看购物车 3.删除商品 4.结算");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    add(stock, shop);
                    break;
                case 2:
                    check(shop);
                    break;
                case 3:
                    delete(shop);
                    break;
                case 4:
                    settleAccount(shop);
                    return;
                default:
                    System.out.println("输入格式错误，请重新输入");
            }
        }
    }


    public static void add(goods[] stock, shoppingCart[] shop) {
        System.out.println("请输入商品名称：");
        Scanner sc = new Scanner(System.in);
        String name1 = sc.nextLine();
        for (int i = 0; i < stock.length; i++) {
            if (name1.equals(stock[i].getName())) {
                for (int j = 0; j < shop.length; j++) {
                    if (shop[j] == null) {
                        shop[j] = new shoppingCart();
                        shop[j].name = name1;
                        shop[j].type = stock[i].getType();
                        shop[j].price = stock[i].getPrice();
                        System.out.println("请添加商品数量：");
                        int num = sc.nextInt();
                        if (num <= (stock[i].getCount())) {
                            shop[j].count = num;
                            System.out.println("添加成功！");
                            break;
                        } else {
                            System.out.println("库存不足");
                            break;
                        }
                    } else if (j == (shop.length - 1)) {
                        System.out.println("购物车已满。");
                        break;
                    }
                }
            }
        }
    }

        public static void check (shoppingCart[]shop){
            System.out.println("-------------------购物车-------------------\n");
            for (int i = 0; i < shop.length; i++) {
                if (shop[i] != null) {
                    System.out.println("商品名称: " + shop[i].getName() + ", 类型: " + shop[i].getType() + ", 价格: " + shop[i].getPrice() + ", 数量: " + shop[i].getCount());
                } else {
                    break;
                }
            }
        }

        public static void delete (shoppingCart[]shop){
            System.out.println("请输入要删除的商品名称：");
            Scanner sc = new Scanner(System.in);
            String name2 = sc.nextLine();
            for (int i = 0; i < shop.length; i++) {
                if (shop[i] != null && name2.equals(shop[i].getName())) {
                    for (int j = i; j < shop.length - 1; j++) {
                        shop[j] = shop[j + 1];
                    }
                    shop[shop.length - 1] = null;
                    System.out.println("删除成功！");
                    break;
                }
            }
        }

        public static void settleAccount (shoppingCart[]shop){
            System.out.println("==================================================");
            double total = 0;
            for (int i = 0; i < shop.length; i++) {
                if (shop[i] != null) {
                    total += (shop[i].getPrice()) * (shop[i].getCount());
                } else {
                    break;
                }
            }
            for (int i = 0; i < shop.length; i++) {
                if (shop[i] != null) {
                    System.out.println("商品名称: " + shop[i].getName() + ", 类型: " + shop[i].getType() + ", 价格: " + shop[i].getPrice() + ", 数量: " + shop[i].getCount());
                } else {
                    break;
                }
            }
            System.out.println("总价：" + total + "元");
        }
    }

